package com.jkdroid.maya.helper;

import android.os.SystemClock;

/**
 * 处理多次点击事件的帮助类
 * Created by guanzhihao on 2015/12/15.
 */
public class MultiClickHelper {

    /**
     * 双击
     */
    public static final int DOUBLE_CLICK = 2;

    /**
     * 三击
     */
    public static final int TRIPLE_CLICK = 3;

    private static final int DEFAULT_INTERVAL = 500;
    private final long[] mHits;

    /**
     * 传入要设定的点击次数，如双击则传入2
     * @param times
     */
    public MultiClickHelper(int times){
        if(times < 1){
            throw new IllegalArgumentException("times should large than 0");
        }
        this.mHits = new long[times];
    }

    /**
     * 当达到了设置的多次点击次数时会返回true
     * @return
     */
    public boolean onClick() {
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        return mHits[0] >= (SystemClock.uptimeMillis() - DEFAULT_INTERVAL) ? true : false;
    }
}
